<?php
class Common_CountryController extends Zend_Controller_Action
{


	public function indexAction(){
        if($this->_request->isPost()){
            $page = $this->_request->getParam('page',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $cn_name = $this->_request->getParam('cn_name','');
            $en_name = $this->_request->getParam('en_name','');
			$page = ($page<1)?1:$page;
            $condition = array(
                'cn_name'       =>$cn_name,
                'en_name'       => $en_name,
            );
            $countryList= Common_Service_Country::getByCondition($condition,'*','sort',$page,$pageSize);
            $count = Common_Service_Country::getByCondition($condition,'count(*)');
            $json =array('list'=>$countryList,'count'=>$count);
            echo json_encode($json);
            exit;

        }else{
            echo Cff::renderTpl("common/country/index.tpl","layout");
        }
    }

    public function infoAction(){
        $country_id = $this->_request->getParam('country_id',0);
        try{
            $country = Common_Service_Country::getByValue($country_id);
            $return = array(
                'ask' =>true,
                'list' =>$country,
            );
        }catch(Exception $e){
            $return = array(
                'ask' =>false,
                'list' =>null,
            );
        }
        echo json_encode($return);
        exit;
    }


    public function editAction(){
        $return = array();

        $country_id = $this->_request->getParam('country_id',0);
        $cn_name = $this->_request->getParam('cn_name','');
        $en_name = $this->_request->getParam('en_name','');
        $local_name = $this->_request->getParam('local_name','');

        $country_alias = $this->_request->getParam('country_alias','');
        $country_code = $this->_request->getParam('country_code','');
        $sort = $this->_request->getParam('sort',0);
        $short_name = $this->_request->getParam('short_name','');


        $val =array(
            'country_id' =>$country_id,
            'cn_name' =>$cn_name,
            'en_name' =>$en_name,
            'local_name' =>$local_name,
            'country_alias' =>$country_alias,
            'country_code' =>$country_code,
            'sort' =>$sort,
            'short_name' =>$short_name,
        );

        $error = Common_Service_Country::validator($val);
        if(!empty($error)){
            $return['ask'] =false;
            $return['list']['error'] =$error;
            echo Zend_Json::encode($return);
            exit ;
        }
        unset($val['country_id']);
        if($country_id>0){

            if(Common_Service_Country::update($val,$country_id)){
                $return['ask'] =true;
                $return['list']['message'] =$this->lang['country']."{$cn_name}".$this->lang['update'].$this->lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$this->lang['country']."{$cn_name}".$this->lang['update'].$this->lang['fail'];
            }
        }else{
            if(Common_Service_Country::add($val)){
                $return['ask'] =true;
                $return['list']['message'][] =$this->lang['country']."{$cn_name}".$this->lang['add'].$this->lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$this->lang['country']."{$cn_name}".$this->lang['add'].$this->lang['fail'];
            }
        }
        echo Zend_Json::encode($return);
        exit ;
    }
    public function deleteAction(){
        $return = array();
        $country_id = $this->_request->getParam('country_id',0);
        $country = Common_Service_Country::getByValue($country_id);
        if($country){
            if(Common_Service_Country::delete($country_id)){
                $return['ask'] =true;
                $return['list']['message'] = $this->lang['country']."{$country['cn_name']}".$this->lang['delete'].$this->lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] = $this->lang['country']."{$country['cn_name']}".$this->lang['delete'].$this->lang['fail'];
            }
        }else{
                $return['ask'] =false;
                $return['list']['error'][] = $this->lang['data'].$this->lang['not_exists'].$this->lang['delete'].$this->lang['fail'];
        }
        echo Zend_Json::encode($return);
        exit;
    }
}